<?php

class Activity_Model_Activity
{
	protected $_id = NULL;
	protected $_time = NULL; //!< The time the action performed
	protected $_action = NULL; //!< The action itself
	protected $_subject = NULL; //!< The performer
	protected $_objects = NULL; //!< Objects used by the subject
	protected $_params = NULL;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }


	public function setId($activityId = NULL)
	{
		$this->_id = $activityId;
		return $this;
	}
	public function setTime($time = NULL)
	{
		$this->_time = $time;
		return $this;
	}
	public function setAction($action = NULL)
	{
		$this->_action = $action;
		return $this;
	}
	public function setObjects($objects = NULL)
	{
		$this->_objects = $objects;
		$this->_subject = NULL;
		return $this;
	}
	public function setParams($params = NULL)
	{
		$this->_params = $params;
		return $this;
	}

	public function getId()
	{
		return $this->_id;
	}
	public function getTime()
	{
		return $this->_time;
	}
	public function getAction()
	{
		return $this->_action;
	}
	public function getSubject()
	{
		if ($this->_subject instanceof Euso_Object) {
			return $this->_subject;
		}
		$objects = $this->getObjects();
		foreach ($objects as $obj) {
			if ($obj->getRole() == 'subject') {
				$this->_subject = $obj;
				return $this->_subject;
			}
		}
		$this->_subject = NULL;
		return $this->_subject;
	}
	public function getObjects()
	{
		//TODO: check
		if ($this->_objects === NULL || is_array($this->_objects) === FALSE) {
			$this->_objects = Activity_Model_ObjectRef::getInstancesByActivityId($this->getId());
		}
		return $this->_objects;
	}
	public function getParams()
	{
		return $this->_params;
	}


    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        $this->$method($value);
    }
    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }


	protected static $_instanceList = array();


	public static function getNewests($count = 10)
	{
		$instances = array();


		$table = Activity_Model_DbTable_Activity::getInstance();

		//TODO: filters
		$select = $table->select();
		$select
			->order('id DESC') //TODO: by time and then by id
			->limit($count)
		;

		$dbResult = $table->fetchAll($select);

		foreach ($dbResult as $dbRow) {
			if (isset(self::$_instanceList[$dbRow->id])) {
				$instances[] = self::$_instanceList[$dbRow->id];
			} else {
				$instance = new Activity_Model_Activity();
				$instance
					->setId($dbRow->id)
					->setTime($dbRow->time)
					->setAction($dbRow->action)
				;

				self::$_instanceList[$dbRow->id] = $instance;
				$instances[] = $instance;
			}
		}


		return $instances;
	}

	public static function getNewestsByObject($classIdName, $objectId, $count=20)
	{
		$instances = array();


		$objects = Activity_Model_ObjectRef::getInstancesByObject($classIdName, $objectId);
		if (is_array($objects) == FALSE || count($objects) < 1) {
			return array();
		}


		$selectWhere = $objects[0]->getActivityId();
		foreach ($objects as $obj) {
			$selectWhere .= ','. $obj->getActivityId();
		}


		$table = Activity_Model_DbTable_Activity::getInstance();

		//TODO: filters
		//TODO: array in sql?
		$select = $table->select();
		$select
			->order('id DESC') //TODO: by time and then by id
			->limit($count)
			->where('id IN ('. $selectWhere .') ')
		;

		//TODO: remove the redudancy
		foreach ($objects as $obj) {
			$select->orWhere('id= ?', $obj->getActivityId());
		}

		$dbResult = $table->fetchAll($select);

		foreach ($dbResult as $dbRow) {
			if (isset(self::$_instanceList[$dbRow->id])) {
				$instances[] = self::$_instanceList[$dbRow->id];
			} else {
				$instance = new Activity_Model_Activity();
				$instance
					->setId($dbRow->id)
					->setTime($dbRow->time)
					->setAction($dbRow->action)
				;

				self::$_instanceList[$dbRow->id] = $instance;
				$instances[] = $instance;
			}
		}


		return $instances;
	}
}
